#!/bin/bash

cat "./scripts/env.sh" > "./scripts/jalias_env.sh"
sed -i '' "s#%pwd%#$(pwd)#g" ./scripts/jalias_env.sh

printf "\033[32m%s\033[0m\n" "● [jalias] init env success [1/4]"

# shellcheck source=/dev/null
source "$(pwd)/scripts/jalias_env.sh" "1"

chmod +x "${JAliasSrc:?}/scripts/install.sh"
chmod +x "${JAliasSrc:?}/scripts/jalias.sh"
chmod +x "${JAliasSrc:?}/scripts/uninstall.sh"
mkdir -p "${JAliasStorePath:?}"

printf "\033[32m%s\033[0m\n" "● [jalias] store path create ok ($JAliasStorePath) [2/4]"

# register env
# shellcheck source=/dev/null
source "${JAliasSrc:?}/scripts/tool.bash"
rc_file="$(shell_rc_file)"
check=$(grep "source ${JAliasSrc}/scripts/jalias_env.sh" "$rc_file")
if [ -z "$check" ]; then
    echo "source $JAliasSrc/scripts/jalias_env.sh" >>"$rc_file"
fi

printf "\033[32m%s\033[0m\n" "● [jalias] register to $rc_file success [3/4]"

# shellcheck disable=SC1090
source "$rc_file"

printf "\033[32m%s\033[0m\n" "● [jalias] reload $rc_file success [4/4]"

# rm -rf "${JAliasSrc:?}/doc/*"

printf "\033[32m%s\033[0m\n" "● jalias install ok!"